package com.herench.bs.support;

import com.herench.bs.core.TokenProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import java.util.Map;


/**
 * Created by ThinkPad on 2017/8/24.
 */

public abstract class AbstractMessageHandler implements MessageHandler {

    private CleverClinicContext cleverClinicContext;
    private String functionCode;
    private String handlerName;



    public CleverClinicContext getCleverClinicContext() {
        return this.cleverClinicContext;
    }

    @Autowired
    public void setCleverClinicContext(CleverClinicContext cleverClinicContext) {
        this.cleverClinicContext = cleverClinicContext;
    }

    public String getFunctionCode() {
        return functionCode;
    }

    public AbstractMessageHandler() {
    }

    public AbstractMessageHandler(String functionCode, String handlerName) {
        this.functionCode = functionCode;
        this.handlerName = handlerName;
    }

    public void setFunctionCode(String functionCode) {
        Assert.notNull(functionCode, "functionCode，不能为空.");
        this.functionCode = functionCode;
    }

    public void setHandlerName(String handlerName) {
        this.handlerName = handlerName;
    }

    public String getHandlerName() {
        return handlerName;
    }


    @Override
    public ResponseMessage handler(final RequestMessage requestMessage) throws Exception {
        Assert.notNull(this.getFunctionCode(), "handler 的functionCode 为空");
        String funcode = requestMessage.getFuncode();
        String captcha = requestMessage.getCaptcha();

        if (getCleverClinicContext().getContextConfig().getDeployEv().equals(DeployEvEnum.ut.name()) || getCleverClinicContext().getContextConfig().getDeployEv().equals(DeployEvEnum.publish.name())) {
            if (!this.cleverClinicContext.getGreenChannelCodes().contains(funcode)) {
                boolean flag = TokenProvider.checkTokenLifeCycleIsLive(captcha);
                if (!flag) {
                    throw new RuntimeException("captcha 失效,请重新验证.");
                }
            }
        }
        if (this.getFunctionCode().equals(funcode)) {
            return this.innerHandler(requestMessage);
        } else {
            throw new RuntimeException(this.getFunctionCode() + "messageHandler,被错误的指定为:" + funcode + "处理");
        }
    }

    protected abstract ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception;


    @Override
    public String getFunctionName() {
        return handlerName;
    }

    @Override
    public void setFunctionName(String functionName) {
        this.handlerName = functionName;
    }

    protected void getPropertiesFromInvokerResultMap(ResponseMessage responseMessage, Map<String, Object> result) {
        String code = (String) result.get("code");
        String messageIn = (String) result.get("messageIn");
        String messageOut = (String) result.get("messageOut");
        String message = (String) result.get("message");
        responseMessage.setCode(code);
        result.remove("code");
        responseMessage.setMessage(message);
        result.remove("message");
        responseMessage.setMessageIn(messageIn);
        result.remove("messageIn");
        responseMessage.setMessageOut(messageOut);
        result.remove("messageOut");
    }

}
